# -*- coding: utf-8 -*-
from flask import current_app
from info.models import Task, Schedule
from info.utils import utils
import datetime
from info import db


def write_data_to_db(task_id, user_addr, server_addr):
    from manager import app

    s = Schedule()
    s.id = utils.random_uuid()
    s.task_id = task_id
    s.create_time = s.last_update_time = datetime.datetime.now()
    s.user_ip = user_addr
    s.server_ip = server_addr
    with app.app_context():
        try:
            db.session.add(s)
        except Exception as e:
            current_app.logger.error(e)
        else:
            db.session.commit()

        current_app.logger.info(f"未执行完的记录添加成功")
